package ${generatorConfig.serviceGeneratorConfig.targetPackage};

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import ${generatorConfig.entityGeneratorConfig.targetPackage}.${table.entityName};
import ${generatorConfig.mapperGeneratorConfig.targetPackage}.${table.entityName}Mapper;
import org.sky.sys.mapper.SysCommonMapper;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.sys.util.page.PageListDataUtils;
import org.sky.util.LogUtils;
import org.sky.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
* @author ${generatorConfig.author}
* @Title: ${table.entityName}Service
* @ProjectName ${generatorConfig.serviceGeneratorConfig.targetProject?substring(0,generatorConfig.serviceGeneratorConfig.targetProject?index_of("/"))}
* @Description: TODO{${table.tableComment}业务类}
* @date ${.now}
* @Version 1.0
*/
@Service
public class ${table.entityName}Service {
    private final static Logger log = LogUtils.getLogger(${table.entityName}Service.class);
    @Autowired
    private ${table.entityName}Mapper ${table.entityName?uncap_first}Mapper;
    @Autowired
    private SysCommonMapper sysCommonMapper;
    /**
    * ${table.tableComment}分页查询
    * @param pageNum
    * @param pageSize
    * @param example
    * @return
    */
    public PageListData select${table.entityName}ByPage(int pageNum, int pageSize, Example example){
        PageHelper.startPage(pageNum, pageSize);
        final PageInfo<${table.entityName}> pageInfo = new PageInfo<${table.entityName}>(${table.entityName?uncap_first}Mapper.selectByExample(example));
        return PageListDataUtils.getPageListData().setTotal(pageInfo.getTotal()).setRows(pageInfo.getList());
    }
    /**
    * 保存{新增/修改}${table.tableComment}
    * @param ${table.entityName?uncap_first}
    * @throws ServiceException
    */
    public void saveAddEdit${table.entityName}(${table.entityName} ${table.entityName?uncap_first}) throws ServiceException{
        try {
            Timestamp ts = sysCommonMapper.queryTimestamp();
            if (null == ${table.entityName?uncap_first}.getId()) {
                Long id = SnowflakeIdWorker.getInstance().nextId();
                ${table.entityName?uncap_first}.setId(id);
                ${table.entityName?uncap_first}.setCreater(BspUtils.getLoginUser().getCode());
                ${table.entityName?uncap_first}.setCreateTime(ts);
                ${table.entityName?uncap_first}.setUpdater(BspUtils.getLoginUser().getCode());
                ${table.entityName?uncap_first}.setUpdateTime(ts);
                ${table.entityName?uncap_first}Mapper.insertSelective(${table.entityName?uncap_first});
            } else {
                ${table.entityName?uncap_first}.setUpdater(BspUtils.getLoginUser().getCode());
                ${table.entityName?uncap_first}.setUpdateTime(ts);
                Example e = new Example(${table.entityName}.class);
                e.createCriteria().andEqualTo("id", ${table.entityName?uncap_first}.getId());
                ${table.entityName?uncap_first}Mapper.updateByExampleSelective(${table.entityName?uncap_first},e);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("${table.tableComment}保存失败:{}",e.getMessage());
            throw new ServiceException("${table.tableComment}保存失败:"+e.getMessage(),e);
        }
    }
    /**
    * 根据ID删除${table.tableComment}
    * @param list
    */
    public void del${table.entityName}ById(List<${table.entityName}> list){
        try {
            List idList = new ArrayList();
            for (${table.entityName} ${table.entityName?uncap_first} : list) {
                idList.add(${table.entityName?uncap_first}.getId());
            }
            Example example = new Example(${table.entityName}.class);
            example.createCriteria().andIn("id", idList);
            ${table.entityName?uncap_first}Mapper.deleteByExample(example);
        }catch (Exception e){
            e.printStackTrace();
            log.error("${table.tableComment}删除失败:{}",e.getMessage());
            throw new ServiceException("${table.tableComment}删除失败:"+e.getMessage(),e);
        }
    }
    /**
    * 根据Id查询${table.tableComment}
    * @param id
    * @return
    */
    public ${table.entityName} get${table.entityName}ById(Long id){
        Example example = new Example(${table.entityName}.class);
        example.createCriteria().andEqualTo("id",id);
        return ${table.entityName?uncap_first}Mapper.selectOneByExample(example);
    }
}
